
#ifndef __BITMAP_H__
#define __BITMAP_H__


#define  BITMAP_SUB_MAX      8
#define  BITMAP_LOC_INVALID  0xffffffff
#define  BITMAP_LOC_INVALID_NEW  0
#define  BITMAP_VAL_INVALID_NEW  0
#define  RESULT_COUNT_MAX    1000
#define  BIT_TEST(uiValue, uiLoc)  (0 != ((uiValue) & (0x80000000U >> (uiLoc))))

#define  BITMAP_MATCH_MAXCNT     (31)
#define  BITMAP_INVALID_CNT      (0)


typedef struct tagBitmap
{
    unsigned int uiLoc;
    unsigned int uiValue;
} BITMAP_S;

typedef struct tagBitmapEx
{
    unsigned int uiCount;
    BITMAP_S astBitmap[0];
} BITMAP_EX_S;

typedef struct tagKindBitmap
{
    unsigned int uiBitmapSubCnt;
    unsigned int uiBitmapUintCnt;
    BITMAP_EX_S *astSubBitmap[BITMAP_SUB_MAX];
} KIND_BITMAP_S;

typedef struct tagBitmapMatchEx
{
    unsigned int uiCount;
    BITMAP_S *pstBitmap;
} BITMAP_MATCH_EX_S;

typedef struct tagKindBitmapMatch
{
    unsigned int uiBitmapSubCnt;
    unsigned int uiBitmapUintCnt;
    BITMAP_MATCH_EX_S astSubBitmap[BITMAP_SUB_MAX];
} KIND_BITMAP_MATCH_S;


unsigned int BITMAP_EX_First_CompareOpt(KIND_BITMAP_MATCH_S* pstKindMatchArray, unsigned int uiKindCount,
    unsigned int uiMinKindPos, unsigned int uiMaxKindPos, unsigned int auiResult[RESULT_COUNT_MAX]);

unsigned int BITMAP_EX_First_CompareUltra(KIND_BITMAP_MATCH_S* pstKindMatchArray, unsigned int uiKindCount,
    unsigned int uiMinKindPos, unsigned int uiMaxKindPos, unsigned int uiHeadMaxLocPos, unsigned int auiResult[RESULT_COUNT_MAX]);

unsigned int BITMAP_EX_All_CompareOpt(KIND_BITMAP_MATCH_S* pstKindMatchArray, unsigned int uiKindCount,
    unsigned int uiMinKindPos, unsigned int uiMaxKindPos, unsigned int auiResult[RESULT_COUNT_MAX]);

static inline unsigned int BITMAP_FIRST(unsigned int uiBitmapValue)
{
    if (uiBitmapValue & 0xffff0000)
    {
        if (uiBitmapValue & 0xff000000)
        {
            if (uiBitmapValue & 0xf0000000)
            {
                if (uiBitmapValue & 0xC0000000)
                {
                    if (uiBitmapValue & 0x80000000)
                    {
                        return 1;
                    }
                    else//0x40000000
                    {
                        return 2;
                    }
                }
                else//0x30000000
                {
                    if (uiBitmapValue & 0x20000000)
                    {
                        return 3;
                    }
                    else//0x10000000
                    {
                        return 4;
                    }
                }
            }
            else//0x0f000000
            {
                if (uiBitmapValue & 0x0C000000)
                {
                    if (uiBitmapValue & 0x08000000)
                    {
                        return 5;
                    }
                    else//0x04000000
                    {
                        return 6;
                    }
                }
                else//0x03000000
                {
                    if (uiBitmapValue & 0x02000000)
                    {
                        return 7;
                    }
                    else//0x01000000
                    {
                        return 8;
                    }
                }
            }
        }
        else//0x00ff0000
        {
            if (uiBitmapValue & 0x00f00000)
            {
                if (uiBitmapValue & 0x00C00000)
                {
                    if (uiBitmapValue & 0x00800000)
                    {
                        return 9;
                    }
                    else//0x00400000
                    {
                        return 10;
                    }
                }
                else//0x00300000
                {
                    if (uiBitmapValue & 0x00200000)
                    {
                        return 11;
                    }
                    else//0x00100000
                    {
                        return 12;
                    }
                }
            }
            else//0x000f0000
            {
                if (uiBitmapValue & 0x000C0000)
                {
                    if (uiBitmapValue & 0x00080000)
                    {
                        return 13;
                    }
                    else//0x00040000
                    {
                        return 14;
                    }
                }
                else//0x00030000
                {
                    if (uiBitmapValue & 0x00020000)
                    {
                        return 15;
                    }
                    else//0x00010000
                    {
                        return 16;
                    }
                }
            }
        }
    }
    else if (uiBitmapValue & 0x0000ffff)
    {
        if (uiBitmapValue & 0x0000ff00)
        {
            if (uiBitmapValue & 0x0000f000)
            {
                if (uiBitmapValue & 0x0000C000)
                {
                    if (uiBitmapValue & 0x00008000)
                    {
                        return 17;
                    }
                    else//0x00004000
                    {
                        return 18;
                    }
                }
                else//0x00003000
                {
                    if (uiBitmapValue & 0x00002000)
                    {
                        return 19;
                    }
                    else//0x00001000
                    {
                        return 20;
                    }
                }
            }
            else//0x00000f00
            {
                if (uiBitmapValue & 0x00000C00)
                {
                    if (uiBitmapValue & 0x00000800)
                    {
                        return 21;
                    }
                    else//0x00000400
                    {
                        return 22;
                    }
                }
                else//0x00000300
                {
                    if (uiBitmapValue & 0x00000200)
                    {
                        return 23;
                    }
                    else//0x00000100
                    {
                        return 24;
                    }
                }
            }
        }
        else//0x000000ff
        {
            if (uiBitmapValue & 0x000000f0)
            {
                if (uiBitmapValue & 0x000000C0)
                {
                    if (uiBitmapValue & 0x00000080)
                    {
                        return 25;
                    }
                    else//0x00000040
                    {
                        return 26;
                    }
                }
                else//0x00000030
                {
                    if (uiBitmapValue & 0x00000020)
                    {
                        return 27;
                    }
                    else//0x00000010
                    {
                        return 28;
                    }
                }
            }
            else//0x0000000f
            {
                if (uiBitmapValue & 0x0000000C)
                {
                    if (uiBitmapValue & 0x00000008)
                    {
                        return 29;
                    }
                    else//0x00000004
                    {
                        return 30;
                    }
                }
                else//0x000000030000
                {
                    if (uiBitmapValue & 0x0002)
                    {
                        return 31;
                    }
                    else//0x00000001
                    {
                        return 32;
                    }
                }
            }
        }
    }
    else
    {
        return 0;
    }
}

#endif